package com.thinkcube.edutainment.fragment;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Background;
import org.androidannotations.annotations.Bean;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.Trace;
import org.androidannotations.annotations.UiThread;
import org.androidannotations.annotations.ViewById;
import org.androidannotations.annotations.rest.RestService;
import org.androidannotations.annotations.sharedpreferences.Pref;

import android.app.Fragment;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.thinkcube.edutainment.EdutainmentConstant;
import com.thinkcube.edutainment.R;
import com.thinkcube.edutainment.ReachabilityTest;
import com.thinkcube.edutainment.adapters.WikiListAdapter;
import com.thinkcube.edutainment.client.UserClient;
import com.thinkcube.edutainment.client.WikiClient;
import com.thinkcube.edutainment.domain.OAuth;
import com.thinkcube.edutainment.domain.WikiList;
import com.thinkcube.edutainment.domain.data.CourseData;
import com.thinkcube.edutainment.domain.dto.CourseDto;
import com.thinkcube.edutainment.domain.dto.OAuthDto;
import com.thinkcube.edutainment.service.SPreferences_;



@EFragment(R.layout.wiki_list)
public class WikiFragment extends Fragment implements OnItemClickListener {

	@ViewById(R.id.list)
	ListView listView;
	
	@ViewById(R.id.wiki_title)
	TextView wikiTitle;
	
	@RestService
	WikiClient wikiClient;

	@Bean
	OAuthDto oAuthDto;

	@RestService
	UserClient userClient;
	
	@ViewById(R.id.progressBar)
	ProgressBar progress;
	
	boolean isFromDashBord;

	CourseData courseData;
	
	@ViewById(R.id.notification_msg)
	TextView noData;
	
	@AfterViews
	void initUi()
	{
		if(getArguments().containsKey("Course"))
		{
			courseData=getArguments().getParcelable("Course");
			if(courseData!=null)
			{
				wikiTitle.setText(courseData.getCourseTitle());
			}
			
			/*check whether infate from dashboard to add dynamic fragment(R.ld.lessoncontet/R.id.pane)*/
			if(getArguments().containsKey("fromDashBord"))
			{
				System.out.println("fromDashBord");
				isFromDashBord=getArguments().getBoolean("fromDashBord");
			}
		}
		
		
		
		gettoken();
	}
	
	
	@Trace
	
	@Background
	void gettoken() {
		oAuthDto.setAppId(EdutainmentConstant.APP_ID);
		oAuthDto.setAppSecret(EdutainmentConstant.APP_SCRET);

		new ReachabilityTest(getActivity(), new ReachabilityTest.Callback() {

			@Override
			public void onReachabilityTestPassed() {

				try {
					getWikiList(userClient.getRequestToken(oAuthDto
							.getMultiValuedMap()));
				} catch (Exception e) {

					showMessge(e.getLocalizedMessage());
				}
			}

			@Override
			public void onReachabilityTestFailed() {

				showMessge("not connected ");
			}
		});
		
		
	}

	@Bean
	CourseDto courseDto;

	@Pref
	SPreferences_ pref;

	@Background
	void getWikiList(OAuth requestToken) {
	
		
		//courseDto.setAuthToken("1234");
		courseDto.setAuthToken(requestToken.getoAuth().getToken());
		courseDto.setCourseId(courseData.getCourseId());

		//courseDto.setUserId(pref.getId().get());

		new ReachabilityTest(getActivity(), new ReachabilityTest.Callback() {

			@Override
			public void onReachabilityTestPassed() {

				try {
					doWikiList(wikiClient.getWikiList(courseDto
							.getMultiValuedMap()));
				} catch (Exception e) {

					System.err.println(e);
				}
			}

			@Override
			public void onReachabilityTestFailed() {

				showMessge("not connected ");
			}
		});

	}
	
	@Bean
	WikiListAdapter wikiListAdapter;
	
	
	WikiList wikiList;
	@UiThread
	void doWikiList(WikiList wikiList) {
		
		this.wikiList=wikiList;
		wikiListAdapter.setWikiListData(wikiList.getWikiList());
		listView.setOnItemClickListener(this);
		listView.setAdapter(wikiListAdapter);
		progress.setVisibility(View.GONE);
		
		if(wikiList!=null)
		{
			if(wikiList.getWikiList()!=null)
			{
				
				if(wikiList.getWikiList().size()==0)
				{
					noData.setVisibility(View.VISIBLE);
				}
				
			}else
			{
				noData.setVisibility(View.VISIBLE);
				
			}
		}
		
		

	}

	@UiThread
	void showMessge(final String msg) {
		Toast.makeText(getActivity(), msg, Toast.LENGTH_LONG).show();
	}
	

	
	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		
		
				FragmentTransaction fragmentTransaction = getFragmentManager()
				.beginTransaction();
		        Bundle bundle=new Bundle();
		        
		        bundle.putString("pageId",wikiList.getWikiList().get(position).getId());
		        
		        bundle.putString("CourseTitle", courseData.getCourseTitle());
		        System.out.println("FROM DASH BORD "+isFromDashBord);
				WikiDetailFragment wikiFragment=new WikiDetailFragment_();
				wikiFragment.setArguments(bundle);
				if(isFromDashBord)
				{
					fragmentTransaction.replace(R.id.pane, wikiFragment);
				}
				else{
					fragmentTransaction.replace(R.id.lesson_content, wikiFragment);
				}
				
				
				fragmentTransaction.addToBackStack(null);
				fragmentTransaction.commit();
		
		
	}
	
	
	
	
	
}
